package com.example.PullGame;

import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.example.MatchGame.MatchGame;
import com.example.MatchGame.MatchOver;
import com.example.TheParent.ParentManager;
import com.example.wonderfulradish.MainActivity;
import com.example.wonderfulradish.R;

import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;


public class PullGame extends AppCompatActivity implements View.OnClickListener{

    private ProgressBar progressBar;
    private Button  leaveGame,pushGame,pointButton;
    private ImageView tutuReset;
    private TextView gamePullTime,scorText;
    private int recLenPull = 20;
    Timer timer = new Timer();
    private ImageView[] luoboPull;
    private ImageView luoboColor;
    public final int RED=0;
    public final int ORANGE=1;
    public final int YELLOW=2;
    public final int BLUE=3;
    public final int PURPLE=4;
    public final int GREEN=5;
    private int score=0;
    private MediaPlayer mp;
    private int flag=1 ,voice=1;
    private Animation pointAnimation;
    private int color ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_game);

        progressBar=(ProgressBar) findViewById(R.id.progressBar);
        leaveGame=(Button) findViewById(R.id.leave_game);
        leaveGame.setOnClickListener(this);
        pushGame=(Button) findViewById(R.id.push_time);
        pushGame.setOnClickListener(this);

        gamePullTime=(TextView) findViewById(R.id.game_time);
        timer.schedule(task2, 1000, 1000);       // 设置游戏线程

        scorText=(TextView)findViewById(R.id.scoretext);
        scorText.setText(score+"/18");
        pointButton=(Button) findViewById(R.id.point_button);
        pointButton.setOnClickListener(this);

        //重绘按钮
        tutuReset=(ImageView) findViewById(R.id.tutu) ;
        tutuReset.setOnClickListener(this);



        luoboPull=new ImageView[9];
        luoboPull[0]=(ImageView)findViewById(R.id.nitu11);
        luoboPull[1]=(ImageView)findViewById(R.id.nitu12);
        luoboPull[2]=(ImageView)findViewById(R.id.nitu13);
        luoboPull[3]=(ImageView)findViewById(R.id.nitu21);
        luoboPull[4]=(ImageView)findViewById(R.id.nitu22);
        luoboPull[5]=(ImageView)findViewById(R.id.nitu23);
        luoboPull[6]=(ImageView)findViewById(R.id.nitu31);
        luoboPull[7]=(ImageView)findViewById(R.id.nitu32);
        luoboPull[8]=(ImageView)findViewById(R.id.nitu33);

        //选中的图片
        luoboColor=(ImageView) findViewById(R.id.select_color);
        luoboColor.setOnClickListener(this);
        //绘制萝卜
        drawluobo();
        mediaplayer();


}

    Random random=new Random();

    private void mediaplayer(){
        mp=MediaPlayer.create(this, R.raw.sugar);
        mp.setLooping(true);
        try {
            //背景音乐准备
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //背景音乐播放
        mp.start();
    }

    //存id
    int imageid[]={R.drawable.orange_tuluobo_big,R.drawable.blue_tuluobo_mid,R.drawable.red_tuluobo_big,
            R.drawable.yellow_tuluobo_big};

    //选择萝卜颜色

    int s=random.nextInt(4);

    //画萝卜
    private void drawluobo(){
        //判断颜色
        if(s==RED){
            luoboColor.setImageResource(R.drawable.red_small);
            for (int i=0;i<luoboPull.length;i++){
                int n=random.nextInt(4);
                luoboPull[i].setImageResource(imageid[n]);
                //如果图片资源为红色则设置红色的图片的点击事件
                if (imageid[n]==R.drawable.red_tuluobo_big){
                    color=i;
                    luoboPull[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int progress=progressBar.getProgress();
                            progress=progress+10;
                            score++;
                            scorText.setText(score+"/18");
                            progressBar.setProgress(progress);
                            if (progress==180||score>=18){
                                intent();
                            }
                            luoboPull[color].clearAnimation();
                            drawluobo();
                        }
                    });
                }else{
                    luoboPull[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int progress=progressBar.getProgress();
                            if (progress>0) {
                                progress = progress - 10;
                                score--;
                                scorText.setText(score+"/18");
                                progressBar.setProgress(progress);
                            }else {
                                intent();
                            }
                            luoboPull[color].clearAnimation();
                            drawluobo();
                        }
                    });
                }
            }
        }else if(s==BLUE){
            luoboColor.setImageResource(R.drawable.blue_small);
            for (int i=0;i<luoboPull.length;i++){
                int n=random.nextInt(4);
                luoboPull[i].setImageResource(imageid[n]);
                if (imageid[n]==R.drawable.blue_luobo_mid){
                    color=i;
                    luoboPull[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int progress=progressBar.getProgress();
                            progress=progress+10;
                            score++;
                            scorText.setText(score+"/18");
                            progressBar.setProgress(progress);
                            if (progress==180||score>=18){
                                intent();
                            }
                            luoboPull[color].clearAnimation();
                            drawluobo();
                        }
                    });
                }else{
                    luoboPull[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int progress=progressBar.getProgress();
                            if (progress>0) {
                                progress = progress - 10;
                                score--;
                                scorText.setText(score+"/18");
                                progressBar.setProgress(progress);
                            }else {
                                intent();
                            }
                            luoboPull[color].clearAnimation();
                            drawluobo();
                        }
                    });
                }
            }
        }else if (s==ORANGE){
            luoboColor.setImageResource(R.drawable.orange_small);
            for (int i=0;i<luoboPull.length;i++){
                int n=random.nextInt(4);
                luoboPull[i].setImageResource(imageid[n]);
                if (imageid[n]==R.drawable.orange_tuluobo_big){
                    color=i;
                    luoboPull[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int progress=progressBar.getProgress();
                            progress=progress+10;
                            score++;
                            scorText.setText(score+"/18");
                            progressBar.setProgress(progress);
                            if (progress==180||score>=18){
                                intent();
                            }
                            luoboPull[color].clearAnimation();
                            drawluobo();
                        }
                    });
                }else{
                    luoboPull[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int progress=progressBar.getProgress();
                            if (progress>0) {
                                progress = progress - 10;
                                score--;
                                scorText.setText(score+"/18");
                                progressBar.setProgress(progress);
                            }else {
                                intent();
                            }
                            luoboPull[color].clearAnimation();
                            drawluobo();
                        }
                    });
                }
            }
        }else if (s==YELLOW){
            luoboColor.setImageResource(R.drawable.yellow_small);
            for (int i=0;i<luoboPull.length;i++){
                int n=random.nextInt(4);
                luoboPull[i].setImageResource(imageid[n]);
                if (imageid[n]==R.drawable.yellow_tuluobo_big){
                    color=i;
                    luoboPull[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int progress=progressBar.getProgress();
                            progress=progress+10;
                            score++;
                            scorText.setText(score+"/18");
                            progressBar.setProgress(progress);
                            if (progress==180||score>=18){
                                intent();
                            }
                            luoboPull[color].clearAnimation();
                            drawluobo();
                        }
                    });
                }else{
                    luoboPull[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int progress=progressBar.getProgress();
                            if (progress>0) {
                                progress = progress - 10;
                                score--;
                                scorText.setText(score+"/18");
                                progressBar.setProgress(progress);
                            }else {
                                intent();
                            }
                            luoboPull[color].clearAnimation();
                            drawluobo();
                        }
                    });
                }
            }
        }
    }


    TimerTask task2 = new TimerTask() {
        @Override
        public void run() {

            runOnUiThread(new Runnable() {      // UI thread
                @Override
                public void run() {
                    recLenPull--;
                    if(recLenPull>=10){
                        gamePullTime.setText("00："+recLenPull);
                    }else{
                        gamePullTime.setText("00:0"+recLenPull);}
                    //画萝卜

                    //如果时间结束，关闭页面
                    if(recLenPull <= 0||score>=18||score<0){
                        mp.stop();
                        intent();
                        finish();
                        //退出线程
                        System.gc();
                        System.exit(0);
                    }
                }
            });
        }
    };

    //结束页面
    private void intent(){
        Intent intent;
        if (score<=0){
            intent=new Intent(PullGame.this,PullOverFail.class);
            mp.stop();
            timer.cancel();
            startActivity(intent);
            finish();
        }else if (score<=3&&score>0){
            intent=new Intent(PullGame.this,PullOverOne.class);
            mp.stop();
            timer.cancel();
            startActivity(intent);
            finish();
        }else if (score<=9){
            intent=new Intent(PullGame.this,PullOverTwo.class);
            mp.stop();
            timer.cancel();
            startActivity(intent);
            finish();
        }else if (score<=18){
            intent=new Intent(PullGame.this,PullOverThree.class);
            mp.stop();
            timer.cancel();
            startActivity(intent);
            finish();
        }else{
            intent=new Intent(PullGame.this,PullOverFail.class);
            mp.stop();
            timer.cancel();
            startActivity(intent);
            finish();
        }
    }

    public void onClick(View v){

        switch (v.getId()){
            case R.id.leave_game:
                //弹框
                final AlertDialog.Builder builder=new AlertDialog.Builder(PullGame.this);
                builder.setTitle("确认信息");
                builder.setMessage("你要退出游戏吗");
                builder.setCancelable(false);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent=new Intent(PullGame.this,Pull_radish.class);
                        finish();
                        mp.stop();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();

                    }
                });
                builder.setCancelable(true);
                builder.show();
                break;
            case R.id.push_time:
                if (flag==1){
                    mp.pause();
                    pushGame.setBackgroundResource(R.drawable.icon_stop_83px);
                    flag=0;
                    timer.cancel();
                }else{
                    mp.reset();
                    pushGame.setBackgroundResource(R.drawable.icon_play);
                    flag=1;
                }

                break;
            case R.id.point_button:
                if (voice==1){
                    pointAnimation= AnimationUtils.loadAnimation(this,R.anim.size_change);
                    luoboPull[color].startAnimation(pointAnimation);
                    voice=0;

                }else{
                    luoboPull[color].clearAnimation();
                    voice=1;

                }
                break;
            case R.id.tutu:
                drawluobo();
                break;
            default:
                break;

        }

    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        mp.release();
    }

}
